var _profileIndex = 0;
var __profileData = {};
var __profileDataArr = [];
var __profileRoot = [];
var __profileRelation = {};

function $jsprofile(__profile, args) {
	var pIndex = __profile[0];
	__profileData[pIndex] = __profile;
	__profileDataArr.push(['[',__profile.join(","),']'].join(""));
	if (args.callee.caller && args.callee.caller.caller) {
		var parentId = args.callee.caller.caller.arguments["__id"];
		if(parentId){
			if(!__profileRelation[parentId])__profileRelation[parentId] = [];
			__profileRelation[parentId].push(pIndex);
		}else{
			__profileRoot.push(pIndex);
		}
	}else{
		__profileRoot.push(pIndex);
	}
}

function __getProfileString(){
	var resultArr = ["{items:["];
	resultArr.push(__profileDataArr.join(","));
	__profileDataArr = [];
	resultArr.push("],root:[");
	resultArr.push(__profileRoot.join(","));
	resultArr.push("],relation:{");
	var relationArr = [];
	for(var relation in __profileRelation){
		var tempArr = [];
		tempArr.push(relation);
		tempArr.push(":[");
		tempArr.push(__profileRelation[relation].join(","));
		tempArr.push("]");
		relationArr.push(tempArr.join(""));
	}
	resultArr.push(relationArr.join(","));
	resultArr.push("}}");
	return resultArr.join("");
}

function __sendProfileData(){
	var xmlHttp = __createXMLHttp();
	xmlHttp.open("POST", "/cgtw.profile?" + new Date(), false);
	xmlHttp.send(__getProfileString());
}
setInterval(__sendProfileData,1000);
function __createXMLHttp() {
	if (typeof XMLHttpRequest != "undefined") {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		var aVersions = ["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0",
				"MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];
		for (var i = 0; i < aVersions.length; i++) {
			try {
				var oXmlHttp = new ActiveXObject(aVersions[i]);
				return oXmlHttp;
			} catch (oError) {

			}
		}
	}
}